package com.example.administrator.printoutindemo.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.di.component.DaggerXiuGaiBeiZhuComponent;
import com.example.administrator.printoutindemo.di.module.XiuGaiBeiZhuModule;
import com.example.administrator.printoutindemo.mvp.contract.XiuGaiBeiZhuContract;
import com.example.administrator.printoutindemo.mvp.presenter.XiuGaiBeiZhuPresenter;
import com.example.administrator.printoutindemo.mvp.ui.entity.XiuGaiBeiZhu;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xw.repo.XEditText;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.simple.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_XIU_GAI_BEI_ZHU)
public class XiuGaiBeiZhuActivity extends BaseActivity<XiuGaiBeiZhuPresenter> implements XiuGaiBeiZhuContract.View {

    @BindView(R.id.et_bei_zhu)
    XEditText etBeiZhu;
    @BindView(R.id.btn_sava)
    Button btnSava;
    private String mMid;
    private String mRemark;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerXiuGaiBeiZhuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .xiuGaiBeiZhuModule(new XiuGaiBeiZhuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_xiu_gai_bei_zhu; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_xiu_gai_bei_zhu));
        //getXiuGaiBeiZhuData();
        mMid = getIntent().getExtras().getString("mid");
        mRemark = getIntent().getExtras().getString("remark");
        etBeiZhu.setText(mRemark);
    }

    @Override
    public void getXiuGaiBeiZhuDataSuccess(XiuGaiBeiZhu entityData) {


    }

    private void getXiuGaiBeiZhuData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getXiuGaiBeiZhuData(map);
    }

    @OnClick(R.id.btn_sava)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etBeiZhu.getText().toString().trim())) {
            showMessage("备注不能为空");
            return;
        }
        updRemarkByMid(mMid, etBeiZhu.getText().toString().trim());
    }

    private void updRemarkByMid(String mid, String remark) {

        showLoading();

        String methodName = "updRemarkByMid";

        SoapParams params = new SoapParams();
        params.put("mid", mid);//用户id
        params.put("remark", remark);//项目名称

        /**
         * 请求
         */
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    showMessage("备注修改成功");
                    EventBus.getDefault().post("", "updateRuKuChengGongJiLu");

                    killMyself();
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());

    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
